package com.hzdl.apple.controller.homeCotroll;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hzdl.apple.bean.Good;
import com.hzdl.apple.service.GoodsService;
@WebServlet("/details/*")
public class DetailsServlet extends HttpServlet {
	GoodsService service;
	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		service = new GoodsService();
		getServletContext().setAttribute("path", getServletContext().getContextPath());
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String baseUri = req.getRequestURI();
		//转发到详情页
		if (baseUri.contains("/toDetails")){
			String GoodsName = req.getParameter("GoodsName");
			List<Good> list = service.getGoods(GoodsName);
			//新建一个list来存放所有的颜色
			List<String> color = new ArrayList<>();
			//把商品集合中的每一个颜色都加入到color集合中
			for (Good good : list) {
				color.add(good.getGoodsColor());
			}
			//去重
			for  ( int  i  =   0 ; i  <  color.size()  -   1 ; i ++ )  {       
			      for  ( int  j  =  color.size()  -   1 ; j  >  i; j -- )  {       
			           if  (color.get(j).equals(color.get(i)))  {       
			        	   color.remove(j);       
			            }
			      }
			}
			//给前端发送
			req.setAttribute("color", color);
			//除了图片不同，其他的东西都是一样的
			req.setAttribute("Goods", list.get(0));
			//跳转
			req.getRequestDispatcher("/WEB-INF/jsp/home/goods-details.jsp").forward(req, resp);
		}else if(baseUri.contains("/toCart")){
			String color = req.getParameter("chooseColor");
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
